package lschgcdev.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class CSUser extends NetBasicIO{

	public DataNet dataNet;
	public int port;
	private Socket socket;
	
	public CSUser(String ID, UserType userType){
		super(ID);
		dataNet = userType.get();
	}
	
	public CSUser(Socket socket) throws IOException{
		this(socket.getInetAddress().getHostAddress(), UserType.TCP);
		
		this.socket = socket;
		dataNet.inetSA = new InetSocketAddress(socket.getInetAddress(), socket.getPort());
		initIO(socket);
		dataNet.service(this);
	}
	
	public void connect(String hostname, int port) throws IOException{
		log.notice("Initializing hostname and port...");
		dataNet.inetSA = new InetSocketAddress(hostname, port);
		
		log.notice("Connecting to "+hostname+":"+port+" ...");
		socket = new Socket(dataNet.inetSA.getAddress(), dataNet.inetSA.getPort());
		
		initIO(socket);
	}
	
	public void close() throws IOException{
		socket.close();
	}
}
